package com.teacold.customdemo.chapter_three;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.teacold.customdemo.R;

import androidx.appcompat.app.AppCompatActivity;

public class PathAnimator extends AppCompatActivity implements View.OnClickListener {
    private Button menu, menu_one, menu_two, menu_four, menu_five, menu_six;
    private boolean mIsMenuOpen = false;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.path_animator_layout);
        initView();
        initMonitor();
    }


    private void initView() {
        menu = findViewById(R.id.menu);
        menu_one = findViewById(R.id.menu_one);
        menu_two = findViewById(R.id.menu_two);
        menu_four = findViewById(R.id.menu_four);
        menu_five = findViewById(R.id.menu_five);
        menu_six = findViewById(R.id.menu_six);
        ValueAnimator valueAnimator=ValueAnimator.ofPropertyValuesHolder();


    }

    private void initMonitor() {
        menu.setOnClickListener(this);
        menu_one.setOnClickListener(this);
        menu_two.setOnClickListener(this);
        menu_four.setOnClickListener(this);
        menu_five.setOnClickListener(this);
        menu_six.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        if (!mIsMenuOpen) {
            mIsMenuOpen = true;
            openMenu();
        } else {
            Toast.makeText(this, "你单击了" + v, Toast.LENGTH_LONG).show();
            mIsMenuOpen = false;
            closeMenu();
        }


    }

    /**
     * 关闭菜单按钮
     */
    private void closeMenu() {
        doAnimateClose(menu_one, 0, 5, 300);
        doAnimateClose(menu_two, 1, 5, 300);
        doAnimateClose(menu_four, 2, 5, 300);
        doAnimateClose(menu_five, 3, 5, 300);
        doAnimateClose(menu_six, 4, 5, 300);

    }

    /**
     * 打开菜单按钮
     */
    private void openMenu() {
        doAnimateOpen(menu_one, 0, 5, 300);
        doAnimateOpen(menu_two, 1, 5, 300);
        doAnimateOpen(menu_four, 2, 5, 300);
        doAnimateOpen(menu_five, 3, 5, 300);
        doAnimateOpen(menu_six, 4, 5, 300);

    }

    private void doAnimateOpen(Button view, int index, int total, int radius) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
        double degree = Math.toRadians(90) / (total - 1) * index;
        int translationX = -(int) (radius * Math.sin(degree));
        int translationY = -(int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        //包含平移、缩放和透明度动画
        set.playTogether(ObjectAnimator.ofFloat(view,"translationX",0,translationX),
                ObjectAnimator.ofFloat(view,"translationY",0,translationY),
                ObjectAnimator.ofFloat(view,"scaleX",0f,1f),
                ObjectAnimator.ofFloat(view,"scaleY",0f,1f),
                ObjectAnimator.ofFloat(view,"alpha",0f,1));
        set.setDuration(500).start();
    }
    private void doAnimateClose(Button view, int index, int total, int radius) {
        if (view.getVisibility() != View.VISIBLE) {
            view.setVisibility(View.VISIBLE);
        }
        double degree = Math.PI * index/((total-1)*2);
        int translationX = -(int) (radius * Math.sin(degree));
        int translationY = -(int) (radius * Math.cos(degree));
        AnimatorSet set = new AnimatorSet();
        //包含平移、缩放和透明度动画
        set.playTogether(ObjectAnimator.ofFloat(view,"translationX",translationX,0),
                ObjectAnimator.ofFloat(view,"translationY",translationY,0),
                ObjectAnimator.ofFloat(view,"scaleX",1f,0f),
                ObjectAnimator.ofFloat(view,"scaleY",1f,0f),
                ObjectAnimator.ofFloat(view,"alpha",1f,0));
        set.setDuration(500).start();
    }


}
